package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.DtFinalResult;
import com.ruoyi.project.system.mapper.DtFinalResultMapper;
import com.ruoyi.project.system.service.IDtFinalResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 查询Controller
 */
@RestController
@RequestMapping("/system/result")
public class DtFinalResultController extends BaseController {

    @Autowired
    private IDtFinalResultService dtFinalResultService;

    @Autowired
    private DtFinalResultMapper dtFinalResultMapper;

    /**
     * 查询查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:result:list')")
    @GetMapping("/list")
    public TableDataInfo list(DtFinalResult dtFinalResult) {
        if (!SecurityUtils.isAdmin()) {
            dtFinalResult.setUserId(SecurityUtils.getUserId());
        }
        startPage();
        List<DtFinalResult> list = dtFinalResultService.selectDtFinalResultList(dtFinalResult);
        return getDataTable(list);
    }

    /**
     * 查询查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:result:list')")
    @GetMapping("/subList")
    public TableDataInfo subList(DtFinalResult dtFinalResult) {
        dtFinalResult.setUserId(SecurityUtils.getUserId());
        startPage();
        List<DtFinalResult> list = dtFinalResultMapper.selectSubAccountDtFinalResultList(dtFinalResult);
        return getDataTable(list);
    }

    /**
     * 获取查询详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:result:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        DtFinalResult result = dtFinalResultService.selectDtFinalResultById(id);
        Long userId = SecurityUtils.getUserId();
        if (result.getUserId().longValue() == userId.longValue()){
            return success(result);
        } else {
            return warn("非法请求");
        }
    }

    /**
     * 修改查询
     */
    @PreAuthorize("@ss.hasPermi('system:result:edit')")
    @Log(title = "查询", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DtFinalResult finalResultInfo) {
        return toAjax(dtFinalResultService.updateDtFinalResultInfo(finalResultInfo));
    }

    /**
     * 删除查询
     */
    @PreAuthorize("@ss.hasPermi('system:result:remove')")
    @Log(title = "查询", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(dtFinalResultService.deleteDtFinalResultByIds(ids));
    }
}
